package sburb;

import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.Sys;

import java.lang.Math;

public class Image {

	String filepath = "";
	int x = 0;
	int y = 0;
	boolean Render = false;
	private Texture texture;
	static TextureLoader texloader = new TextureLoader();

	//Create and render Image.
	public Image(String img, int xpos, int ypos) {
		this(img,xpos,ypos,true);
	}
	
	//Create Image (and render only if specified).
	public Image(String img, int xpos, int ypos, boolean autorender) {
		filepath = img;
		x = xpos;
		y = ypos;
		
		try {
			texture = texloader.getTexture(img);
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (autorender) {
			start();
		}
	}
	

	//Start rendering the Image.
	public void start() {
		Render = true;
		//renderImage();
	}

	//Stop rendering the Image.
	public void stop() {
		Render = false;
	}

	/**
	 * draw a quad with the Image on it
	 */
	public void drawTexture() {
		if (!Render) return;
		texture.bind(); // or GL11.glBind(texture.getTextureID());
		double curtime = System.currentTimeMillis()/1000.0*Math.PI;
		int x = this.x+(int)(Math.sin(curtime)*25.0);
		int y = this.y+(int)(Math.cos(curtime)*25.0);
		
		GL11.glBegin(GL11.GL_QUADS);
			GL11.glTexCoord2f(0,0);
			GL11.glVertex2f(x,y);
			GL11.glTexCoord2f(1,0);
			GL11.glVertex2f(x+texture.getImageWidth(),y);
			GL11.glTexCoord2f(1,1);
			GL11.glVertex2f(x+texture.getImageWidth(),y+texture.getImageHeight());
			GL11.glTexCoord2f(0,1);
			GL11.glVertex2f(x,y+texture.getImageHeight());
		GL11.glEnd();
	}
}